import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/a.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/widgets/bottom_sheet/depreciation_method_select_bottom_sheet.dart';
import 'package:finance_app/src/widgets/pop/drow_down_menu.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';

class NormalBluePop<T> extends StatefulWidget {
  final T select;
  final List<T> group;
  final String hint;
  final ValueChanged<T> onSelect;
  final bool isTop;
  final double horizontalPadding;
  final BuildDisplayName<T> buildDisplayName;

  const NormalBluePop(
      {Key key,
      this.horizontalPadding,
      this.select,
      this.group,
      this.hint,
      this.onSelect,
      this.isTop = false, this.buildDisplayName})
      : super(key: key);

  @override
  _NormalBluePopState<T> createState() => _NormalBluePopState<T>();
}

class _NormalBluePopState<T> extends State<NormalBluePop<T>> {
  DropDownMenuController controller = DropDownMenuController();
  T _select;

  @override
  void initState() {
    super.initState();
    _select = widget.select;
    controller.addListener(() {
      setState(() {});
    });
  }

  @override
  void didUpdateWidget(covariant NormalBluePop oldWidget) {
    if (widget.select != _select) {
      setState(() {
        _select = widget.select;
      });
    }
    super.didUpdateWidget(oldWidget);
  }

  @override
  void dispose() {
    super.dispose();
    controller.dispose();
  }

  Widget _buildIndustryItem(T e) {
    return PressedOpacityButton(
      onPressed: () {
        widget.onSelect?.call(e);
        controller.close();
        setState(() {
          _select = e;
        });
      },
      color: e == _select ? aceWhiteGreyColor : null,
      padding: EdgeInsets.symmetric(horizontal: getDp(15), vertical: getDp(19)),
      child: Text(
        widget.buildDisplayName?.call(e),
        style: TextStyle(
          color: aceBlackColor,
          fontSize: getSp(16),
          fontWeight: FontWeight.w700,
          height: 1,
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.symmetric(
        horizontal: widget.horizontalPadding ?? getDp(25),
      ),
      child: DropDownMenu(
        isTop: widget.isTop,
        isStackChild: true,
        controller: controller,
        dropDownMenu: WillPopScope(
          onWillPop: () async {
            controller.close();
            return false;
          },
          child: Container(
            clipBehavior: Clip.antiAliasWithSaveLayer,
            decoration: BoxDecoration(
              color: Colors.white,
              boxShadow: [
                BoxShadow(
                  color: Colors.black.withOpacity(0.25),
                  blurRadius: getDp(22),
                  spreadRadius: -getDp(10),
                ),
              ],
              borderRadius: BorderRadius.circular(getDp(15)),
            ),
            child: Column(
              children: [
                if (widget.isTop == true) ...[
                  Container(
                    constraints: BoxConstraints(
                      maxHeight: getDp(280),
                    ),
                    child: ListView.builder(
                      shrinkWrap: true,
                      padding: EdgeInsets.zero,
                      itemCount: widget.group.length,
                      itemBuilder: (BuildContext context, int index) =>
                          _buildIndustryItem(widget.group[index]),
                    ),
                  ),
                  Divider(
                    height: 1,
                  ),
                  PressedOpacityButton(
                      onPressed: () {
                        controller.close();
                      },
                      child: buildSelectInput(isPop: true)),
                ] else ...[
                  PressedOpacityButton(
                      onPressed: () {
                        controller.close();
                      },
                      child: buildSelectInput(isPop: true)),
                  Divider(
                    height: 1,
                  ),
                  Container(
                    constraints: BoxConstraints(
                      maxHeight: getDp(280),
                    ),
                    child: ListView.builder(
                      shrinkWrap: true,
                      padding: EdgeInsets.zero,
                      itemCount: widget.group.length,
                      itemBuilder: (BuildContext context, int index) =>
                          _buildIndustryItem(widget.group[index]),
                    ),
                  )
                ]
              ],
            ),
          ),
        ),
        child: buildSelectInput(isPop: controller.isOpen),
      ),
    );
  }

  Widget buildSelectInput({bool isPop = false}) {
    return Container(
        padding: EdgeInsets.symmetric(
          horizontal: getDp(12),
        ),
        height: getDp(54),
        decoration: BoxDecoration(
          color: Colors.white,
          boxShadow: isPop
              ? []
              : [
                  BoxShadow(
                    color: Colors.black.withOpacity(0.25),
                    blurRadius: getDp(22),
                    spreadRadius: -getDp(10),
                  ),
                ],
          borderRadius: isPop
              ? widget.isTop == true
                  ? BorderRadius.only(
                      bottomLeft: Radius.circular(getDp(15)),
                      bottomRight: Radius.circular(getDp(15)),
                    )
                  : BorderRadius.only(
                      topLeft: Radius.circular(getDp(15)),
                      topRight: Radius.circular(getDp(15)),
                    )
              : BorderRadius.circular(getDp(15)),
        ),
        child: Row(
          children: [
            Expanded(
                child: _select == null
                    ? Text(
                        widget.hint,
                        style: TextStyle(
                          color: aceGrayColor,
                          fontSize: getSp(14),
                          fontWeight: FontWeight.w700,
                          height: 16 / 14,
                        ),
                      )
                    : Text(
                        widget.buildDisplayName?.call(_select)??'Staff',
                        style: TextStyle(
                          color: aceBlackColor,
                          fontSize: getSp(16),
                          fontWeight: FontWeight.w700,
                          height: 1,
                        ),
                      )),
            SvgPicture.asset(
              A.assetsImagesStyleArrowBlack,
              width: getDp(7),
              height: getDp(7),
              fit: BoxFit.contain,
            ),
          ],
        ));
  }
}
